package com.neveu.android.ds.groupmessenger;

import java.util.Hashtable;
import java.util.Vector;

public class VectorClock {

	private final int numProcesses;
//	private int[] clockVals;
//	private Hashtable<Integer, Integer> clock_;
	private Vector<Integer> clock_;
	
	public VectorClock(int size)
	{
		numProcesses = size;
		clock_ = new Vector<Integer>(size);
		for(int i = 0; i< size; i++)
		{
			clock_.add(0);
		}
	}
		
	public Vector<Integer> getClock()
	{
		/* serialization of clock_ is flakey. Values >1 come across as 1 when deserialized. 
		 * Copying values into new Vector instance is a workaround. */
//		return new Vector<Integer>(clock_);

		return clock_;
	}
	
	public static int getSum(Vector<Integer> cl)
	{
		int sum = 0;
		for(int i = 0; i < cl.size(); i++)
			sum += cl.get(i);
		
		return sum;
	}
	
	public void incrementClock(int index)
	{	
		clock_.set(index, clock_.get(index) + 1);
		
//		if(!clock_.contains(id))
//			return;
		
//		clock_.put(id, clock_.get(id).intValue() + 1);
	}

//	public int incrementClock(int index)
//	{
//		if(index >= numProcesses)
//			return -1;
//		
//		return ++clockVals[index];
//	}
}
